/**
 * Created by no1 on 2015/5/18.
 */
'use strict';

var mysql = require('../common/mysql.js');
var M = require('./index.js');
var F = require('../common/function');

// 用户地址信息

function userAddress(aid, uid, addr) {
    this.set(aid, uid, addr);
}

userAddress.prototype.get = function (aid) {
    return {
        aid: this.aid,
        uid: this.uid,
        addr: this.addr
        // status: this.status
    }
};

userAddress.prototype.set = function (aid, uid, addr) {
    this.uid = uid || 0;
    this.aid = aid || 0;
    this.province_id = addr['province_id'] || 0;
    this.province = addr['province'] || '';
    this.city_id = addr['city_id'] || 0;
    this.city = addr['city'] || '';
    this.district_id = addr['district_id'] || 0;
    this.district = addr['district'] || '';
    this.street_id = addr['street_id'] || 0;
    this.street = addr['street'] || '';
    this.address = addr['address'] || '';
    this.username = addr['username'] || '';
    this.mobile = addr['mobile'] || '';
    this.postcode = addr['postcode'] || '';
    this.status = addr['status'] || '';
    return M.returnData(0, '', this.get(aid));
};

userAddress.prototype.delete = function *() {
    if (this.aid > 0 && this.uid > 0) {
        let result = yield mysql.query('update mh_user_address set status=0 where aid=? and uid=?', [this.aid, this.uid]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '删除失败，只能删除自己的收货地址');
        }
        else {
            return M.returnData(0, '删除成功');
        }
    }
};

userAddress.prototype.save = function *() {
    if (this.aid > 0) { // 更新地址
        let result = yield mysql.query('UPDATE mh_user_address SET ' +
            ' province_id=?,' +
            ' province=?,' +
            ' city_id=?,' +
            ' city=?,' +
            ' district_id=?,' +
            ' district=?,' +
            ' street_id=?,' +
            ' street=?,' +
            ' address=?,' +
            ' username=?,' +
            ' mobile=?,' +
            ' postcode=?,' +
            ' status=?' +
            ' WHERE aid=? and uid=?', [
            this.province_id,
            this.province,
            this.city_id,
            this.city,
            this.district_id,
            this.district,
            this.street_id,
            this.street,
            this.address,
            this.username,
            this.mobile,
            this.postcode,
            this.status,
            this.aid,
            this.uid
        ]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '更新失败，只能修改自己的收货地址');
        }
        else {
            return M.returnData(0, '更新成功');
        }
    } else { // 新地址
        let result = yield mysql.query('INSERT INTO mh_user_address(' +
            'uid,province_id,province,city_id,city,district_id,district,street_id,street,address,mobile,postcode,username,status' +
            ') VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,1)', [
            this.uid,
            this.province_id,
            this.province,
            this.city_id,
            this.city,
            this.district_id,
            this.district,
            this.street_id,
            this.street,
            this.address,
            this.mobile,
            this.postcode,
            this.username
        ]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '保存失败');
        }
        else {
            this.aid = result.insertId;
            return M.returnData(0, '保存成功');
        }
    }
};


userAddress.prototype.setDefaultAddress = function *() {
    let threadid = yield mysql.queryOne('select aid from mh_user_address where aid=?', [this.aid]);
    if (!threadid) {
        return M.returnData(1, '参数帖子id在数据库中不存在');
    }
    let result = yield mysql.query('UPDATE mh_user_info SET defaultaddress=? WHERE uid=?', [this.aid, this.uid]);
    if (result.affectedRows == 0) {
        return M.returnData(1, '保存失败');
    }
    else {
        return M.returnData(0, '保存成功');
    }
};

function userAddressList() {
    this.uid = 0;
    this.defaultAddress = 0;
    this.shippingAddress = [];
}

userAddressList.prototype.get = function* (uid) {
    if (uid) { //传参
        uid = parseInt(uid);
        if (uid > 0) { //参数有效
            this.uid = uid;
            let sql = 'SELECT mui.defaultaddress FROM mh_user_info mui WHERE mui.uid=?';
            let result = yield mysql.queryOne(sql, [this.uid]);
            if (result) {
                // 设置默认地址
                this.defaultAddress = result['defaultaddress'];
                sql = 'SELECT * FROM mh_user_address mua WHERE mua.uid=? AND mua.status=1';
                result = yield mysql.query(sql, [this.uid]);
                if (result) {
                    for (var i in result) {
                        result[i].citycode = F.city2code(result[i].city);
                        this.shippingAddress.push(new userAddress(
                            result[i]['aid'], this.uid, result[i]));
                        delete this.shippingAddress[i].uid;
                        //delete this.shippingAddress[i].province;
                    }
                }
            }
        } else //参数无效
            return M.returnData(1, '无效的用户id')
    }
    return M.returnData(0, '获取成功', {
        uid: this.uid,
        defaultAddress: this.defaultAddress,
        shippingAddress: this.shippingAddress
    });
};

exports.userAddress = userAddress;
exports.userAddressList = userAddressList;